package com.example.demo.controller;

//import com.example.demo.common.Result;
//import com.example.demo.entity.User;
//import com.example.demo.mapper.UserMapper;
//
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.annotation.Resource;

import com.example.demo.common.Result;
import com.example.demo.common.ResultObject;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserMapper userMapper;

    @PostMapping("/login")
    public Result<User> login(@RequestBody User user) {
        if (!checkParam(user)) {
            return Result.error("-1", "缺少必要参数");
        }

        User dbUser = userMapper.selectByUsernameAndPassword(user);
        if (dbUser == null) {
            return Result.error("-1", "账号或密码错误");
        }
        return Result.success(dbUser);
    }
    @RequestMapping("/loginOut")
    public ResultObject<Object> loginOut(HttpServletRequest request) {
        //查询用户列表
        ResultObject<Object> rs=new ResultObject<Object>();
        request.getSession().removeAttribute("user");
        //数据
        rs.setCode(com.example.demo.domain.Constant.SUCCESS_RETUEN_CODE);
        rs.setMsg("退出成功");
        return rs;
    }

    private boolean checkParam(User user) {
        return user.getUsername() != null && user.getPassword() != null;
    }
}
